<?php
require_once "class.db.php";

class sanpham extends db {
    /* SAN PHAM */

    function SanPham_Them(&$loi) {

        $thanhcong = true;
        $idTL = (int) $_POST['idTL'];
        $idLT = (int) $_POST['idLT'];

        $MaSP = $this->processData($_POST['MaSP']);
        $MaSP_KD = $this->changeTitle($MaSP);
        $Gia = $this->processData($_POST['Gia']);
        $BaoHanh = $this->processData($_POST['BaoHanh']);
        $XuatXu = $this->processData($_POST['XuatXu']);

        $hinh_dai_dien = $this->processData($_POST['hinh_dai_dien']);
        $hinh_anh = $this->processData($_POST['hinh_anh']);

        $arrHinh = explode(';', $hinh_anh);

        $TinhNang = $_POST['TinhNang'];
        $MoTa = $_POST['MoTa'];

        $AnHien = ($_POST['AnHien'] == "on") ? 0 : 1;
        $NoiBat = ($_POST['NoiBat'] == "on") ? 1 : 0;
        $BanChay = ($_POST['BanChay'] == "on") ? 1 : 0;

        $ngay = strtotime('now');

        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "INSERT INTO sanpham
					VALUES(NULL,'$MaSP','$MaSP_KD','$Gia','$BaoHanh','$XuatXu',
					'$TinhNang','$MoTa','$AnHien','$NoiBat','$BanChay',
                    '$ngay','$hinh_dai_dien',$idTL,$idLT)";
            mysql_query($sql) or die(mysql_error() . $sql);
            $idSP = mysql_insert_id();
            if ($idSP > 0 && !empty($arrHinh)) {
                foreach ($arrHinh as $url) {
                    $this->addImages($url, $idSP);
                }
            }
        }
        return $thanhcong;
    }

    function addImages($url, $idSP) {
        $sql = "INSERT INTO hinhsp VALUES(NULL,'$url',$idSP)";
        mysql_query($sql);
    }

    function SanPham_Sua($sp_id, &$loi) {
        settype($sp_id, "int");
        $thanhcong = true;
        $idLT = (int) $_POST['idLT'];
        $idTL = (int) $_POST['idTL'];

        $MaSP = $this->processData($_POST['MaSP']);
        $MaSP_KD = $this->changeTitle($MaSP);
        $Gia = $this->processData($_POST['Gia']);
        $BaoHanh = $this->processData($_POST['BaoHanh']);
        $XuatXu = $this->processData($_POST['XuatXu']);

        $hinh_dai_dien = $this->processData($_POST['hinh_dai_dien']);
        $hinh_anh = $this->processData($_POST['hinh_anh']);

        $arrHinh = explode(';', $hinh_anh);

        $TinhNang = $_POST['TinhNang'];
        $MoTa = $_POST['MoTa'];

        $AnHien = (int) $_POST['AnHien'];
        $NoiBat = (int) $_POST['NoiBat'];
        $BanChay = (int) $_POST['BanChay'];

        $ngay = strtotime('now');

        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "UPDATE sanpham SET
                                MaSP = '$MaSP',MaSP_KD = '$MaSP_KD',Gia = '$Gia',
                                BaoHanh = '$BaoHanh',XuatXu = '$XuatXu',
                                TinhNang = '$TinhNang',MoTa ='$MoTa',
                                AnHien = '$AnHien',NoiBat = '$NoiBat',
                                BanChay = '$BanChay',ngay = '$ngay',UrlHinh = '$hinh_dai_dien',idLT = $idLT,idTL = $idTL					
                                WHERE idSP = $sp_id";
            mysql_query($sql) or die(mysql_error() . $sql);
            if (!empty($arrHinh)) {
                foreach ($arrHinh as $url) {
                    $this->addImages($url, $sp_id);
                }
            }
        }
        return $thanhcong;
    }

    function getDetailSP($sp_id) {
        $sql = "SELECT * 
                FROM sanpham
                WHERE idSP = $sp_id";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function SanPham_List($loai_id = -1, $tukhoa = '', $limit = -1, $offset = -1) {
        $sql = "SELECT * FROM sanpham 
                WHERE (idTL = $loai_id OR $loai_id = -1 )";
        if ($tukhoa != "")
            $sql.=" AND MaSP LIKE '%$tukhoa%' ";
        $sql.="	ORDER BY idSP DESC ";
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

}

?>